/*
 * @Project Name: spring-boot
 * @File Name: RoleController
 * @Package Name: com.hhly.spring.boot.cache.controller.system
 * @Date: 2018/8/8 10:23
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.system.Role;
import com.sm.shoplook.service.system.PermissionService;
import com.sm.shoplook.service.system.RoleService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.system.permission.PermissionMenuRespVo;
import com.sm.shoplook.vo.system.role.RolePageSearchReqVo;

/**
 * @author chenpy
 * @desc 角色
 * @date 2018/8/8 10:23
 * @see
 */
@RequestMapping("/role")
@Controller
public class RoleController {

	private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);
	@Autowired
	private RoleService roleService;
	@Autowired
	private PermissionService permissionService;

	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String roleManager() {
		return "/role/role_manager";
	}

	@RequestMapping(value = "/roleListPage", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult<Map<String, Object>> roleListPage(RolePageSearchReqVo rolePageSearchReqVo) {
		Map<String, Object> map = new HashMap<>();
		ApiResult<PageInfo<Role>> pageInfoApiResult = roleService.selectRoleListPage(rolePageSearchReqVo);
		PageInfo<Role> pageInfo = pageInfoApiResult.getData();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return new ApiResult<Map<String, Object>>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	@RequestMapping(value = "/save_role_page/{roleId}", method = RequestMethod.GET)
	public String addOrUpdateRolePage(@PathVariable(value = "roleId") Integer roleId, Model model) {
		LOGGER.info("roleId: {}", roleId);
		ApiResult<Role> roleApiResult = roleService.selectRoleById(roleId);
		Role role = roleApiResult.getData();
		if (role == null) {
			role = new Role();
		}
		model.addAttribute("role", role);
		return "/role/add_update_role";
	}

	@RequiresPermissions("role_delete")
	@ResponseBody
	@RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
	public ApiResult deleteRole(String ids) {
		String[] idList = ids.split(",");
		// 批量删除
		LOGGER.info("ids", ids);
		try {
			return roleService.deleteRoles(idList);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	@RequiresPermissions({ "role_add", "role_update" })
	@ResponseBody
	@RequestMapping(value = "/saveRole", method = RequestMethod.POST)
	public ApiResult saveRole(Role role) {
		LOGGER.info("role", role);
		try {
			return roleService.saveRole(role);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * 获取所有的权限角色信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAllPermissions", method = RequestMethod.GET)
	public ApiResult<List<PermissionMenuRespVo>> selectAllPermissions() {
		ApiResult<List<PermissionMenuRespVo>> listApiResult = permissionService.selectAllPermissions();
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, listApiResult.getData());
	}

	/**
	 * 获取角色赋予的权限信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectRoleMenuPermissions", method = RequestMethod.GET)
	public ApiResult<List<String>> selectRoleMenuPermissions(String roleCode) {
		ApiResult<List<String>> listApiResult = roleService.selectRoleMenuPermissions(roleCode);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, listApiResult.getData());
	}

	@RequiresPermissions("role_saver_role_permission")
	@ResponseBody
	@RequestMapping(value = "/saveRolePermission", method = RequestMethod.POST)
	public ApiResult saveRolePermission(String menuCodes, String permissionCodes, String roleCode) {
		if (StringUtils.isEmpty(roleCode)) {
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
		try {
			return roleService.saveRolePermission(menuCodes, permissionCodes, roleCode);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}
}
